package com.example.dianbiao

import android.os.Bundle
import android.util.Log
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import com.example.dianbiao.ui.theme.DianBiaoTheme
import com.google.gson.Gson
import com.google.gson.GsonBuilder
import okhttp3.ResponseBody
import org.jsoup.Jsoup
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import retrofit2.converter.simplexml.SimpleXmlConverterFactory

class MainActivity : ComponentActivity() {

    private val serviceUrl = "https://ehall.xidian.edu.cn:443/login?service=https://ehall.xidian.edu.cn/new/index.html"

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            DianBiaoTheme {
                // A surface container using the 'background' color from the theme
                Surface(
                    modifier = Modifier.fillMaxSize(),
                    color = MaterialTheme.colorScheme.background
                ) {
                    Greeting("Android")
                }
            }
        }

        val retrofit = Retrofit.Builder()
            .baseUrl("http://ids.xidian.edu.cn/")
            .addConverterFactory(ToStringConverterFactory())
            .addConverterFactory(GsonConverterFactory.create())
            .build()

        val loginService = retrofit.create(LoginService::class.java)


        loginService.firstGet(serviceUrl).enqueue(object: Callback<ResponseBody>{
            override fun onResponse(call: Call<ResponseBody>, response: Response<ResponseBody>) {
                Log.d("dianbiao-main", "code: ${response.code()}")
                val page = response.body().string()
                if(page != null){
                    val jsoup = Jsoup.parse(page, "lxml")
                    Log.d("dianbiao-main", "jsoup: $jsoup")
                }
                Log.d("dianbiao-main", "page: $page")
                val names = response.headers().names().toString()
                val location = response.headers().get("Location")
                Log.d("dianbiao-main", "headers: $names")
                Log.d("dianbiao-main", "location: $location")
            }

            override fun onFailure(call: Call<ResponseBody>, t: Throwable) {
                t.printStackTrace()
            }
        })




    }


}

@Composable
fun Greeting(name: String) {
    Text(text = "Hello $name!")
}

@Preview(showBackground = true)
@Composable
fun DefaultPreview() {
    DianBiaoTheme {
        Greeting("Android")
    }
}